package controller

import (
	"github.com/gin-gonic/gin"
	"learning/gooop/spring/demo/framework/bean/controller"
	"learning/gooop/spring/demo/order/dto"
	"learning/gooop/spring/demo/order/entity"
	"learning/gooop/spring/demo/order/service"
	"net/http"
)

// OrderController_Enhanced handles rest requests for CRUD orders
// RestController
// RequestMapping path=/order
type OrderController_Enhanced struct {
	// Autowired
	orderService service.IOrderService
}

// SetOrderService is auto generated setter method for injecting service.IOrderService into me.orderService
func (me *OrderController_Enhanced) SetOrderService(it interface{}) {
	me.orderService = it.(service.IOrderService)
}

// Save create or update an order
// PostMapping
func (me *OrderController_Enhanced) Save(head *entity.OrderHeadEntity, items []*entity.OrderItemEntity) error {
	return me.orderService.Save(head, items)
}

// OrderController_Save_ParamsDTO is auto generated struct for wrapping parameters of OrderController.Save
type OrderController_Save_ParamsDTO struct {
	Order *entity.OrderHeadEntity
	Items []*entity.OrderItemEntity
}

// View_Enhanced is the enhanced version of Save
func (me *OrderController_Enhanced) Save_Enhanced(c *gin.Context) {
	r := new(OrderController_Save_ParamsDTO)
	e := c.BindJSON(r)
	if e != nil {
		c.JSON(http.StatusBadRequest, gin.H{"ok": false, "error": e.Error()})
		return
	}

	e = me.Save(r.Order, r.Items)
	if e != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "error": e.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"ok": true})
}

// View gets order and order items
// GetMapping
func (me *OrderController_Enhanced) View(orderID int) (error, *dto.OrderDTO) {
	return me.orderService.Get(orderID)
}

// View_Enhanced is the enhanced version of View
func (me *OrderController_Enhanced) View_Enhanced(c *gin.Context) {
	id := c.GetInt("id")
	e, d := me.View(id)
	if e != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "error": e.Error()})
	}

	c.JSON(http.StatusOK, d)
}

// Query query order headers by custom conditions
// GetMapping
func (me *OrderController_Enhanced) Query(customerID int, statusFlag int, dateFrom string, dateTo string, pageNO int, pageSize int) (error, []*dto.OrderHeadDTO) {
	return me.orderService.Query(customerID, statusFlag, dateFrom, dateTo, pageNO, pageSize)
}

// OrderController_Query_ParamsDTO is auto generated struct for wrapping parameters of PagedQuery
type OrderController_Query_ParamsDTO struct {
	CustomerID int
	StatusFlag int
	DateFrom   string
	DateTO     string
	PageNO     int
	PageSize   int
}

// Query_Enhanced is the enhanced version of PagedQuery
func (me *OrderController_Enhanced) Query_Enhanced(c *gin.Context) {
	r := new(OrderController_Query_ParamsDTO)
	e := c.Bind(r)
	if e != nil {
		c.JSON(http.StatusBadRequest, gin.H{"ok": false, "error": e.Error()})
		return
	}

	e, d := me.Query(r.CustomerID, r.StatusFlag, r.DateFrom, r.DateTO, r.PageNO, r.PageSize)
	if e != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"ok": false, "error": e.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"ok": true, "data": d})
}

// RegisterRestController is auto generated to implements controller.IRestController interface
func (me *OrderController_Enhanced) RegisterRestController(r *gin.Engine) {
	r.POST("/order/save", me.Save_Enhanced)
	r.GET("/order/view", me.View_Enhanced)
	r.GET("/order/query", me.Query_Enhanced)
}

// init is auto generated to register OrderController_Enhanced into controller.ControllerBeanContext
func init() {
	c := controller.ControllerBeanContext
	it := new(OrderController_Enhanced)
	it.SetOrderService(c.Get("MockOrderService"))
	controller.ControllerBeanContext.Set("OrderController_Enhanced", it)
}
